package com.lmz.project.redis.interview;

import java.util.Arrays;

public class MergeSort {

    public static void main(String[] args) {
        int[] arr = {1, 3, 5, 2, 4};
        int leftIndex = 0;
        int rightIndex = arr.length - 1;
        int[] temp = new int[arr.length];
//        splitArr(arr,leftIndex,rightIndex,temp);
//        System.out.println(Arrays.toString(arr));
        separateArr(arr, leftIndex, rightIndex, temp);
        System.out.println(Arrays.toString(arr));
    }

    //分
    public static void splitArr(int[] arr, int leftIndex, int rightIndex, int[] temp) {
        if (leftIndex < rightIndex) {
            int midIndex = (leftIndex + rightIndex) / 2;
            splitArr(arr, 0, midIndex, temp); //左侧
            splitArr(arr, midIndex + 1, rightIndex, temp); //右侧
            //合并
            mergeArr(arr, leftIndex, midIndex, rightIndex, temp);
        }

    }


    public static void mergeArr(int[] arr, int leftIndex, int midIndex, int rightIndex, int[] temp) {
        int i = leftIndex;
        int j = midIndex + 1;
        int t = 0;
        //先将两个部分合并
        while (i <= midIndex && j <= rightIndex) {
            if (arr[i] <= arr[j]) {
                temp[t] = arr[i];
                i++;
                t++;
            } else {
                temp[t] = arr[j];
                j++;
                t++;
            }
        }
        //如果左边部分没有合并
        while (i <= midIndex) {
            temp[t] = arr[i];
            i++;
            t++;
        }
        //如果右边部分没有合并
        while (j <= rightIndex) {
            temp[t] = arr[j];
            j++;
            t++;
        }
        t = 0;
        //接着将temp里面的数组填充到数组的指定位置
        int tempLeft = leftIndex;
        while (tempLeft <= rightIndex) {
            arr[tempLeft] = temp[t];
            tempLeft++;
            t++;
        }
    }


    //分离
    public static void separateArr(int[] arr, int left, int right, int[] temp) {
        if (left < right) {
            int mid = (left + right) / 2;
            //继续分离左侧数组
            separateArr(arr, 0, mid, temp);
            //继续分离右侧数组
            separateArr(arr, mid + 1, right, temp);
            //合并
            mergeSort(arr, left, mid, right, temp);
        }

    }

    public static void mergeSort(int[] arr, int left, int mid, int right, int[] temp) {
        int i = left;
        int j = mid + 1;
        int t = 0;
        while (i <= mid && j <= right) {
            if (arr[i] < arr[j]) {
                temp[t] = arr[i];
                i++;
                t++;
            } else {
                temp[t] = arr[j];
                j++;
                t++;
            }
        }

        while (i <= mid) {
            temp[t] = arr[i];
            t++;
            i++;
        }

        while (j <= right) {
            temp[t] = arr[j];
            t++;
            j++;
        }
        t = 0;
        //接着将temp里面的数组填充到数组的指定位置
        int tempLeft = left;
        while (tempLeft <= right) {
            arr[tempLeft] = temp[t];
            tempLeft++;
            t++;
        }
    }
}
